package com.suyx.job.service.impl;


import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.suyx.job.dao.TuInfoLogRepository;
import com.suyx.job.entity.JobLog;
import com.suyx.job.entity.Page;
import com.suyx.job.entity.TuInfoLog;
import com.suyx.job.service.ITuInfoLogService;
import com.suyx.job.service.base.impl.MyServiceImpl;

import lombok.extern.slf4j.Slf4j;

/**
 * @Auther: lanyanhua
 * @Date: 2018/10/18 0018 11:50
 * @Description:
 */
@Slf4j
@Service
public class TuInfoLogServiceImpl extends MyServiceImpl<TuInfoLog,Integer> implements ITuInfoLogService {
	
	private TuInfoLogRepository tuInfoLogRepository;

    @Autowired
    public TuInfoLogServiceImpl(TuInfoLogRepository repository) {
        super(repository);
        this.tuInfoLogRepository = repository;
    }

	@Override
	public Page findTuPage(TuInfoLog tuInfoLog, Integer page, Integer limit) {
		return  new Page(
				tuInfoLogRepository.findAll((Root<TuInfoLog> root, CriteriaQuery<?> query, CriteriaBuilder cb) -> {
                    List<Predicate> predicates = new ArrayList<>();

                    if(!StringUtils.isEmpty(tuInfoLog.getRoleName())){
                        predicates.add(cb.like(root.get("roleName").as(String.class), "%"+tuInfoLog.getRoleName() + "%"));
                    }
                    return  query.where(predicates.toArray(new Predicate[0]))
                                .orderBy(cb.desc(root.get("createTime")))
                                .getRestriction();
                }, PageRequest.of(page,limit))
        );
	}
}
